/**
 *  sem.h - Semaphore Mechanism.
 *
 *  Copyright (C) 2008-2010 ZhangHu
 *  All rights reserved.
 *  E-MAIL: anmnmnly@gmail.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#ifndef __SEM_H__
#define __SEM_H__

#include "include/types.h"

#ifndef UNIT_DEBUG
#include "include/list.h"
#include "include/core.h"
#include "include/mem.h"
#include "include/irq.h"
#endif


#ifndef semid_t
#define semid_t  uint32
#endif


typedef struct semaphore {
    int32 sem_cnt;             // value.
    uint32 task_cnt;           // Count all blocked tasks.
    slist_t tcb_link;           // links the locked TCB.
    list_t sem_link;            // All semaphores linked by this member.
    list_t task_link[MAX_PRIO]; // Waiting queue.
} sem_t;


// APIs
// Creating a semaphore.
extern sem_t *semCreate(sem_t *pSemaphore, uint32 InitCnt);

// Creating a semaphore.
extern sem_t *semInit(uint32 InitCnt);

// Creating a mutex semaphore.
extern sem_t *semInitMutex(void);

// Get semaphore.
extern void semGain(sem_t *pSemaphore);

// Get semaphore without block.
extern bool_t semGainFree(sem_t *pSemaphore);

// Unlock a semaphore.
extern void semPost(sem_t *pSemaphore);

// Deleting a semaphore
extern void semDel(sem_t *pSemaphore);


#endif

